package top.smallway.lightbandassistant_app.adapter;


import static top.smallway.lightbandassistant_app.method.Method.secondsToTimeFormat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.UI.AboutSong;
import top.smallway.lightbandassistant_app.data.Song_Data;

/**
 * 歌曲列表适配器
 * 负责显示搜索结果的歌曲列表，实现歌曲项的展示和点击查看详情功能
 */
public class SongsAdapter extends RecyclerView.Adapter<SongsAdapter.MyViewHolder> {
    /**
     * 构造函数
     * @param data 歌曲数据列表
     * @param context 上下文对象
     */
    public SongsAdapter(List<Song_Data> data, Context context) {
        this.data = data;
        this.context = context;
    }

    // 歌曲数据列表
    private List<Song_Data> data;
    // 上下文对象
    private final Context context;
    // 是否启用加载更多功能
    private boolean mIsLoadMoreEnabled = false;
    // 播放器状态常量
    private final int PLAYER_PLAYING = 0;
    private final int PLAYER_STOP = 1;
    private final int PLAYER_STATIC = PLAYER_STOP;


    @NonNull
    /**
     * 创建ViewHolder
     * @param parent 父视图组
     * @param viewType 视图类型
     * @return 返回创建的ViewHolder
     */
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.songs_items, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"ResourceAsColor", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Song_Data song_data = data.get(position);
        holder.songname.setText(song_data.getSong_name());
        holder.songname.setFocusable(true);
        if (song_data.getAuthorName().isEmpty()) {
            holder.authername.setText("未知谱师");
        } else {
            holder.authername.setText(song_data.getAuthorName());
        }
        holder.authername.setFocusable(true);
        holder.key.setText("歌曲KEY：" + song_data.getSong_id());
        Glide.with(holder.itemView.getContext()).load(song_data.getCoverURL()).placeholder(R.drawable.pgbar).error(R.drawable.loadingerror).into(holder.SongPicture);
        holder.inter.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("MissingInflatedId")
            @Override
            public void onClick(View v) {
                holder.itemView.getContext().startActivity(AboutSong.fromDataToIntent(holder.itemView.getContext(),
                        song_data));
            }

        });
    }

    @SuppressLint("NotifyDataSetChanged")
    /**
     * 更新歌曲数据
     * @param newData 新的歌曲数据列表
     */
    public void updateData(List<Song_Data> newData) {
        if (data == null) {
            data = new ArrayList<>();
        }
        Log.d("data", data.toString());
        data.addAll(newData);
        notifyDataSetChanged();
    }

    /**
     * 设置是否启用加载更多功能
     * @param enabled 是否启用
     */
    public void setLoadMoreEnabled(boolean enabled) {
        mIsLoadMoreEnabled = enabled;
        notifyDataSetChanged();
    }

    /**
     * 清空歌曲数据
     */
    public void clearData() {
        if (data != null) {
            data.clear();
        }
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mIsLoadMoreEnabled ? (data == null ? 1 : data.size() + 1) : (data == null ? 0 : data.size());
    }

    /**
     * 视图持有者类
     * 用于缓存歌曲项的视图组件
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private final ImageView SongPicture;
        private final TextView songname;
        private final TextView authername;
        private final TextView key;
        private final LinearLayout inter;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            SongPicture = itemView.findViewById(R.id.SongPicture);
            songname = itemView.findViewById(R.id.songname);
            authername = itemView.findViewById(R.id.authername);
            key = itemView.findViewById(R.id.key);
            inter = itemView.findViewById(R.id.inter);

        }
    }
}
